from firm_offer.model import user_real


class UserRealDao:

    def select_run_user(self, phone):
        """
        查找用户有无正在运行的实盘
        :param phone:
        :return:
        """
        try:
            user_id = user_real.select_running_user(phone)
            real_id = user_real.select_running_real(user_id)
            return real_id
        except Exception as e:
            print(e)
            return None

    def select_old_user(self, user_name):
        """
        查询是否是老用户
        :return:
        """
        try:
            user_id = user_real.select_old_real(user_name)
            return user_id
        except MemoryError as e:
            print(e)

    def insert_about_data(self, data_dict):
        strategy_real_data = data_dict["data"]
        try:
            annual_return = data_dict["base_data"]["stat"]["annual_algo_return"]["value"][0]
        except:
            annual_return = 0
        position_data = data_dict["position_data"]
        # TODO 处理strategy_real_data
        real_id = data_dict.get("real_id")
        if real_id:
            # TODO 这里是新用户的数据
            strategy_real_data["real_id"] = data_dict["real_id"]
            strategy_real_data["annual_return"] = annual_return
            strategy_real_data["cash"] = position_data["cash"]
            strategy_real_data["equity"] = position_data["totalValue"]
            user_real.update_strategy_real(strategy_real_data)
            # TODO 处理strategy_real_netvalue
            netvalue_data = data_dict["netvalue_data"]
            netvalue_data["real_id"] = data_dict["real_id"]
            netvalue_data["capital"] = strategy_real_data["capital"]
            netvalue_data["equity"] = strategy_real_data["equity"]
            netvalue_data["cash"] = strategy_real_data["cash"]
            user_real.insert_strategy_real_netvalue(netvalue_data)
        else:
            # TODO 这里是老用户的数据
            phone = data_dict["phone"]
            # TODO 判断用户的的数据是否是第一次添加
            real_id = user_real.select_is_real(phone)
            if real_id:
                strategy_real_data["annual_return"] = annual_return
                strategy_real_data["cash"] = position_data["cash"]
                strategy_real_data["equity"] = position_data["totalValue"]
                strategy_real_data["real_id"] = real_id
                user_real.update_strategy_real(strategy_real_data)
                netvalue_data = data_dict["netvalue_data"]
                netvalue_data["real_id"] = real_id
                netvalue_data["capital"] = strategy_real_data["capital"]
                netvalue_data["equity"] = strategy_real_data["equity"]
                netvalue_data["cash"] = strategy_real_data["cash"]
                user_real.update_stratey_real_netvalue(netvalue_data)
                # TODO 插入中间表， 关联老用户和实盘
            else:
                strategy_real_data["strategy_name"] = "历史1"
                strategy_real_data["user_id"] = data_dict["user_id"]
                strategy_real_data["annual_return"] = annual_return
                strategy_real_data["cash"] = position_data["cash"]
                strategy_real_data["equity"] = position_data["totalValue"]
                real_id = user_real.insert_strategy_real(strategy_real_data)
                netvalue_data = data_dict["netvalue_data"]
                netvalue_data["real_id"] = real_id
                netvalue_data["capital"] = strategy_real_data["capital"]
                netvalue_data["equity"] = strategy_real_data["equity"]
                netvalue_data["cash"] = strategy_real_data["cash"]
                user_real.insert_strategy_real_netvalue(netvalue_data)
                # TODO 插入中间表， 关联老用户和实盘
                user_real.insert_associated(phone, real_id)
        # TODO 处理strategy_real_position
        position_data["real_id"] = real_id
        user_real.insert_strategy_position(position_data)
        # TODO 处理strategy_real_push
        traction_data = data_dict["tranction_data"]
        if not traction_data:
            print("%s今天没有交易记录" % real_id)
        traction_data["real_id"] = real_id
        traction_data["equity"] = position_data["totalValue"]
        user_real.insert_strategy_real_traction(traction_data)


user_real_dao = UserRealDao()
















